package org.example.controller.supplier;

import org.example.entity.Supplier;
import org.example.service.SupplierService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/supplier/deleteSupplier")
public class deleteSupplierController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收超链接传递过来的id值
        String id = req.getParameter("id");
        //确定删除哪个记录
        Supplier supplier = new Supplier();
        supplier.setId(Integer.parseInt(id));  //接收的字符串转换为整型id
        //调用supplier的实现类和方法
        SupplierService supplierService = new SupplierService();
        try {
            if(supplierService.deleteSupplier(supplier)==1){
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('success...'); window.location='/supplier' </script>");
                out.flush();
                out.close();
            }else{
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('failure...'); window.location='/supplier' </script>");
                out.flush();
                out.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
